home *** CD-ROM | disk | FTP | other *** search
- package sheet;
-
- import java.util.EmptyStackException;
- import java.util.Stack;
-
- public final class CellCalc {
- private static final short STACK_DEPTH = 10;
- private static final short SYM_DATA = 1;
- private static final short SYM_OPEN_BR = 2;
- private static final short SYM_CLOSE_BR = 3;
- private static final short SYM_DELIMITER = 4;
- private static final short SYM_OPERATION = 5;
- private static final short SYM_STRING = 6;
- private static final short SYM_COMMA = 7;
- private static final short PUSH_NO = 0;
- private static final short PUSH_OPERATION = 1;
- private static final short PUSH_DATA = 2;
- private static final short PUSH_FUNCTION = 3;
- private static final Operation[] OPS = new Operation[]{new Operation((short)0, (short)0, 4, '+'), new Operation((short)1, (short)0, 4, '-'), new Operation((short)2, (short)2, 2, '+'), new Operation((short)3, (short)2, 2, '-'), new Operation((short)4, (short)2, 3, '*'), new Operation((short)5, (short)2, 3, '/'), new Operation((short)6, (short)3, 5, '\u0000')};
- private Calculator callBack;
- private IntegerStack operations = new IntegerStack((1)null);
- private Stack operands = new Stack();
- private String expression;
- private short last_push;
- private short fargs;
-
- public CellCalc(Calculator var1) {
- this.callBack = var1;
- }
-
- private static short symbolType(char var0) {
- switch (var0) {
- case '\t':
- case '\n':
- case '\r':
- case ' ':
- return 4;
- case '\u000b':
- case '\f':
- case '\u000e':
- case '\u000f':
- case '\u0010':
- case '\u0011':
- case '\u0012':
- case '\u0013':
- case '\u0014':
- case '\u0015':
- case '\u0016':
- case '\u0017':
- case '\u0018':
- case '\u0019':
- case '\u001a':
- case '\u001b':
- case '\u001c':
- case '\u001d':
- case '\u001e':
- case '\u001f':
- case '!':
- case '#':
- case '$':
- case '%':
- case '&':
- case '\'':
- case '.':
- default:
- return 1;
- case '"':
- return 6;
- case '(':
- return 2;
- case ')':
- return 3;
- case '*':
- case '+':
- case '-':
- case '/':
- return 5;
- case ',':
- return 7;
- }
- }
-
- private boolean execute(boolean var1, int var2) throws RuntimeException {
- short var3;
- try {
- var3 = this.operations.pop();
- } catch (EmptyStackException var10) {
- return false;
- }
-
- if (var3 == 7) {
- if (var1) {
- this.operations.push(var3);
- }
-
- return false;
- } else if (var2 > OPS[var3].priority) {
- this.operations.push(var3);
- return false;
- } else {
- switch (OPS[var3].type) {
- case 2:
- try {
- Operand var5 = (Operand)this.operands.pop();
- Operand var4 = (Operand)this.operands.pop();
- this.operands.push(this.callBack.oper(var3, var4, var5));
- break;
- } catch (IllegalArgumentException var8) {
- throw var8;
- } catch (RuntimeException var9) {
- throw new IllegalArgumentException(Operation.OPNAME[var3] + " ops are wrong");
- }
- case 3:
- Operand[] var6 = new Operand[this.fargs];
-
- for(short var7 = 0; var7 < this.fargs; ++var7) {
- var6[this.fargs - var7 - 1] = (Operand)this.operands.pop();
- }
-
- this.fargs = 0;
- this.operands.push(this.callBack.call(Operand.getName((Operand)this.operands.pop()), var6));
- break;
- default:
- this.operands.push(this.callBack.oper(var3, (Operand)this.operands.pop(), (Operand)null));
- }
-
- return true;
- }
- }
-
- private short pushOperand(short var1, short var2) throws IllegalArgumentException {
- if (var1 != var2) {
- Object var3 = null;
- if (this.last_push == 3) {
- Operand var4 = (new CellCalc(this.callBack)).calculate(this.expression, var1, var2);
- this.operands.push(var4);
- ++this.fargs;
- } else {
- Operand var5 = new Operand(this.expression.substring(var1, var2));
- this.operands.push(var5);
- this.last_push = 2;
- }
- }
-
- return (short)(var2 + 1);
- }
-
- public Operand calculate(String var1, short var2, short var3) throws IllegalArgumentException {
- short var4 = var2;
- short var5 = var2;
- short var6 = 0;
-
- while(!this.operands.empty()) {
- this.operands.pop();
- }
-
- this.operations.clear();
- this.last_push = 0;
-
- for(this.expression = var1; var4 < var3; ++var4) {
- char var7 = var1.charAt(var4);
- switch (symbolType(var7)) {
- case 2:
- if (this.last_push == 3) {
- ++var6;
- } else {
- var5 = this.pushOperand(var5, var4);
- if (this.last_push == 2) {
- this.operations.push((short)6);
- this.last_push = 3;
- this.fargs = 0;
- var6 = 1;
- } else {
- this.operations.push((short)7);
- this.last_push = 1;
- }
- }
- break;
- case 3:
- if (this.last_push == 3) {
- --var6;
- }
-
- if (var6 == 0) {
- for(var5 = this.pushOperand(var5, var4); this.execute(false, 0); this.last_push = 2) {
- }
- }
- break label4;
- case 4:
- if (this.last_push != 3) {
- var5 = this.pushOperand(var5, var4);
- }
- break;
- case 5:
- if (this.last_push == 3) {
- break;
- }
-
- var5 = this.pushOperand(var5, var4);
- short var8 = Operation.find(OPS, var7, this.last_push != 2);
-
- try {
- while(this.execute(true, OPS[var8].priority)) {
- this.last_push = 2;
- }
-
- this.operations.push(var8);
- this.last_push = 1;
- if (OPS[var8].type == 1) {
- while(this.execute(true, OPS[var8].priority)) {
- this.last_push = 2;
- }
- }
- break;
- } catch (ArrayIndexOutOfBoundsException var10) {
- throw new IllegalArgumentException("Bad " + String.valueOf(var7));
- }
- case 6:
- if (var4 != var5) {
- throw new IllegalArgumentException("\" inside text");
- }
-
- do {
- ++var4;
- } while(var4 < var3 && symbolType(var1.charAt(var4)) != 6);
-
- if (var4 >= var3) {
- throw new IllegalArgumentException("No ending \"");
- }
-
- ++var4;
- this.pushOperand(var5, var4);
- var5 = var4--;
- break;
- case 7:
- if (var6 == 1) {
- var5 = this.pushOperand(var5, var4);
- }
- }
- }
-
- this.pushOperand(var5, var4);
-
- while(this.execute(true, 0)) {
- }
-
- if (!this.operations.isEmpty()) {
- throw new IllegalArgumentException(Operation.OPNAME[this.operations.pop()] + " doesn't have a match");
- } else {
- return this.callBack.oper((short)0, (Operand)this.operands.pop(), (Operand)null);
- }
- }
- }
-